Imports System
Imports System.Collections.Generic
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Text
Imports System.Windows.Forms

Namespace SerialPortService
	Public Partial Class Form1
        Inherits Form

		Public Sub New()
			InitializeComponent()
		End Sub

        Private WithEvents localService As SerialPortService
		Private chat As String
		Private status As String

        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _
                                                                            Handles MyBase.Load
            Me.Cursor = Cursors.WaitCursor
            lblStatus.Text = "Stopped"
            Try
                localService = New SerialPortService()

            Catch ex As Exception
                Microsoft.VisualBasic.MsgBox("Unable to create service." _
                                 & ex.Message, Microsoft.VisualBasic.MsgBoxStyle.OkOnly _
                                 Or Microsoft.VisualBasic.MsgBoxStyle.Critical, _
                                            "SerialPortService")

            End Try

            Me.Cursor = Cursors.Default
        End Sub
       

        Private Sub btnStart_Click(ByVal sender As Object, ByVal e As EventArgs) _
                                                                    Handles btnStart.Click
            Me.Cursor = Cursors.WaitCursor
            Try
                localService.RegisterService()
                lblStatus.Text = "Listening"
                btnStart.Enabled = False
                btnStop.Enabled = True

            Catch ex As Exception
                Microsoft.VisualBasic.MsgBox("Unable to unregister service." _
                                             & ex.Message, Microsoft.VisualBasic. _
                                             MsgBoxStyle.OkOnly Or Microsoft. _
                                             VisualBasic.MsgBoxStyle.Critical, _
                                             "Serial Port Service")
            End Try

            Me.Cursor = Cursors.Default
        End Sub

        Private Sub btnStop_Click(ByVal sender As Object, ByVal e As EventArgs) _
                                                                    Handles btnStop.Click
            Me.Cursor = Cursors.WaitCursor
            Try
                localService.UnRegister()
                lblStatus.Text = "Stopped"
                btnStart.Enabled = True
                btnStop.Enabled = False

            Catch ex As Exception
                Microsoft.VisualBasic.MsgBox("Unable to unregister service. " _
                                             & Environment.NewLine & "Error details: " _
                                             & ex.Message, Microsoft.VisualBasic. _
                                             MsgBoxStyle.OkOnly Or Microsoft. _
                                             VisualBasic.MsgBoxStyle.Critical, _
                                             "Serial Port Service")
            End Try

            Me.Cursor = Cursors.Default
        End Sub

        Private Sub localService_NewClientConnected(ByVal sender As Object, _
                                                     ByVal e As EventArgs) _
                                                     Handles localService. _
                                                     NewClientConnected

            status = "Connected"
            Me.Invoke(New EventHandler(AddressOf SetStatus))

            chat = "New client connected: " & Environment.NewLine
            Me.Invoke(New EventHandler(AddressOf SetChat))
        End Sub

        Private Sub localService_DataSentToClient(ByVal sender As Object, _
                                                   ByVal e As SerialDataEventArgs) _
                                                   Handles localService.DataSentToClient

            chat = "Server: " & e.Data.Trim() & Environment.NewLine
            Me.Invoke(New EventHandler(AddressOf SetChat))
        End Sub

        Private Sub localService_DataReceivedFromClient(ByVal sender As Object, _
                                                         ByVal e As SerialDataEventArgs) _
                                                         Handles localService. _
                                                         DataReceivedFromClient

            chat = "Client: " & e.Data.Trim() & Environment.NewLine
            Me.Invoke(New EventHandler(AddressOf SetChat))
        End Sub

        Private Sub localService_ClientDisconnected(ByVal sender As Object, _
                                                     ByVal e As EventArgs) _
                                                     Handles localService. _
                                                     ClientDisconnected

            status = "Listening"
            Me.Invoke(New EventHandler(AddressOf SetStatus))

            chat = "Client disconnected." & Environment.NewLine & Environment.NewLine
            Me.Invoke(New EventHandler(AddressOf SetChat))
        End Sub

		Private Sub SetStatus(ByVal sender As Object, ByVal e As EventArgs)
			lblStatus.Text = status
			status = ""
		End Sub

		Private Sub SetChat(ByVal sender As Object, ByVal e As EventArgs)
            lblChat.Text &= chat
			chat = ""
        End Sub

    End Class

End Namespace
